function [outdat] = rt_writedat(siteID, t_interval)
% Write to file the real time raw data preceded by the following 
% place-indicating header

template = {...
'# **************************************************************************\n';
'# ------------------ PROVISIONAL DATA SUBJECT TO REVISION ------------------\n';
'# **************************************************************************\n';
'# Continuous Real-time Marine Water Data from Monitoring Stations (Moorings)\n';
'# MOORING_COTS Database Export:	%s to %s\n';
'# --------------------------------------------------------------------------\n';
'# \n';
'# ** STATION INFORMATION\n';
'# \n';
'# Name:			MUK01_BOTT\n';
'# Location:			\n';
'# 				\n';
'# 				\n';
'# Description:	\n';	
'# Latitude:	\n';		 
'# Longitude:	\n';	
'# MLLW:		\n';	
'# \n';
'# --------------------------------------------------------------------------\n';
'# \n';
'# ** REAL-TIME DATA DICTIONARY\n';
'# \n';
'# All time stamps are in Coordinated Universal Time (UTC).\n';
'# \n';
'# - Columns: PostedUTC, Pressure, Temperature, Conductivity, Salinity, Density, DissolvedOxygenSat, DissolvedOxygen\n';
'# \n';
'# PostedUTC (ISO 8601)	''yyyymmddTHHMMSS''\n';
'# Pressure (db)\n';
'# Temperature (deg C)\n';
'# Conductivity (S/m)\n';
'# Salinity (psu)\n';
'# Density (Sigma-t, kg/m3)\n';
'# DissolvedOxygenSat (mg/L)\n';
'# DissolvedOxygen (mg/L)\n';
'# \n';
'# --------------------------------------------------------------------------\n';
'# \n';
'# ** DATA DISTRIBUTION\n';
'# \n';
'# This dataset is distributed by the Washington State Department of Ecology.\n';
'# \n';
'# Real-time Data:		ftp://www.ecy.wa.gov/eap/Mooring_Raw\n';
'# QAQC''d Data:		ftp://www.ecy.wa.gov/eap/Mooring_ReviewedData\n';
'# \n';
'# --------------------------------------------------------------------------\n';
'# \n';
'# ** CONTACT INFORMATION\n';
'# \n';
'# Washington State Department of Ecology\n';
'# Environmental Assessment Program (EAP) - Marine Waters Unit (Moorings)\n';
'# \n';
'# Ecology Home:		http://www.ecy.wa.gov/ecyhome.html\n';
'# EAP Home:			http://www.ecy.wa.gov/programs/eap/index.html\n';
'# Marine Waters Home:	http://www.ecy.wa.gov/programs/eap/mar_wat/mwm_intr.html\n';
'# Moorings Home:		http://www.ecy.wa.gov/programs/eap/mar_wat/moorings.html\n';
'# \n';
'# --------------------------------------------------------------------------\n';
'# \n';
'# \n';
'# \n';
'# ** END HEADER INFORMATION: LINE 60\n';
''};

% First, confirm that we can get the data

try
    rawdata = rt_rawdat_query(siteID, t_interval);
catch
    h = errordlg('Error executing rt_rawdat_query.m', 'Error getting data');
    exit
end

% construct output filename
fname = strcat(siteID, char(t_interval(1)), '_', char(t_interval(2)), '.csv');
% open output file
[out, msg] = fopen(fname, 'wt');

if strcmp(msg, '') % if successful
    [nrows, ncols] = size(rawdata);
%    fprintf(out, '%s\n', template{1:4});
%    char(strcat(template{:}))
    fprintf(out, strcat(template{:}), datestr(rawdata{1,3}, 0),... 
                                 datestr(rawdata{nrows,3}, 0));
%    fprintf(out, '%s\n', template{6:nrows});
    [junk, col_heds] = strtok(template{25}, 'P');
    col_heds = regexp(col_heds, ',', 'split');
    fprintf(out, '%s\t', col_heds{1});
    fprintf(out, '\t%s', col_heds{2:8});
    fprintf(out, '\n');
    for i = 1:nrows
        fprintf(out, '%s', datestr(rawdata{i,3}, 30));
        for j = 4:5
            fprintf(out, '\t %6.3f', rawdata{i,j});
        end
        for j = 6:7
            fprintf(out, '\t\t%6.3f', rawdata{i,6});
        end
        fprintf(out, '\n');
    end
    fclose(out);
else % Note: confirmed to work while coding
    h = errordlg(strcat('Error opening', fname, ' for writing'),...
                 'File open error');
end